package com.tools.cleanmaster.functionpage;


import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.tools.ad.EntranceType;
import com.tools.ad.util.GARecordUtils;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.cpucool.util.PhoneAccelerateHelper;
import com.tools.cleanmaster.manager.SortProcessManager;
import com.tools.cleanmaster.taskmain.ProcessItem;
import com.tools.cleanmaster.taskmain.TKPubApi;
import com.tools.cleanmaster.ui.card.animator.AnimationListenerAdapter;
import com.tools.cleanmaster.ui.card.animator.AnimatorListenerAdapter;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 短时间耗电过快处理类
 */
public class FunctionBatteryHandler extends FunctionHandler {
    private BatteryStateView batteryScanView;
    private View batteryScanLayout;
    private View batteryDetailsLayout;
    private TextView batteryDetailsAppNum;
    private AppIconHorizontal appsList;
    private ImageView imageView;
    private AppDetailListView appsDetailList;
    private Button mBottomBtn;
    private TextView mFuncTipsView;
    private TextView batteryNotification;

    private ArrayList<ProcessItem> mList;

    @Override
    public void init(Context context, View viewRoot) {
        super.init(context, viewRoot);
        batteryScanView = mViewRoot.findViewById(R.id.battery_state_scan);
        batteryDetailsAppNum = mViewRoot.findViewById(R.id.battery_details_appnum);
        batteryDetailsLayout = mViewRoot.findViewById(R.id.battery_details_layout);
        batteryScanLayout = mViewRoot.findViewById(R.id.battery_scan_layout);
        appsList = mViewRoot.findViewById(R.id.run_apps_icon_list);
        imageView = mViewRoot.findViewById(R.id.function_battery_top_bg);
        appsDetailList = mViewRoot.findViewById(R.id.run_apps_details_lv);
        mBottomBtn = mViewRoot.findViewById(R.id.battery_bottom_btn);
        mFuncTipsView = mViewRoot.findViewById(R.id.app_icon_horizontal_title);
        batteryNotification = mViewRoot.findViewById(R.id.battery_state_scan_notification);
        mFuncTipsView.setVisibility(View.GONE);
        mBottomBtn.setVisibility(View.GONE);
        mBottomBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBottomBtn.setEnabled(false);
                mBottomBtn.setText(R.string.function_battery_bottom_btn);
                startStateHandler();
            }
        });
    }

    @Override
    public void startStateScan() {
        if (handlerListener != null) {
            handlerListener.startScan();
        }
        mViewRoot.setVisibility(View.VISIBLE);
        batteryScanLayout.setVisibility(View.VISIBLE);
        batteryScanView.startScanBattery(FunctionItemSharedPrefs.getBatteryPercent(imContext),
                false, new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        showMessageTop();
                        mList = TKPubApi.getRunningProcesses(imContext, true);
                        if (mList == null || mList.isEmpty()) {
                            GARecordUtils.report(CleanApplication.getInstance(), CleanGAConstant.C_FUNC, CleanGAConstant.A_SHOW,
                                    String.format(Locale.ENGLISH, CleanGAConstant.L_FUNCTION_EMPTY_RESULT, "power"));

                            imContext.getAdSceneManager().load(EntranceType.HOME_POWER_INTERSTITIAL);

                            //结果停留2s后，才执行完成动画
                            imContext.handleMobEmptyDelayMessage(R.id.msg_show_complete_view, 2000L);

                            FunctionItemSharedPrefs.setFunctionItemOverHandingTime(imContext, FunctionItem.BATTERYOVER);
                        } else {

                            mFuncTipsView.setText(imContext.getString(R.string.function_battery_overtext, mList.size() + ""));
                            mFuncTipsView.setVisibility(View.VISIBLE);
                            appsList.setAppsIcons(mList);
                            appsList.setVisibility(View.VISIBLE);
                            if (imageView != null) {
                                imageView.setVisibility(View.VISIBLE);
                            }

                            if (handlerListener != null) {
                                handlerListener.finishScan();
                            }

                            appsList.setIsNeedShowArrow(true);
                        }
                    }

                });
        appsList.setArrowClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (handlerListener != null) {
                    handlerListener.showAppsDetailPage();
                }
                batteryDetailsLayout.setVisibility(View.VISIBLE);
                batteryScanView.onDestroyView();
                batteryScanLayout.setVisibility(View.GONE);
                ArrayList<SortProcessManager.ProcessItem> list = SortProcessManager.getSortProcessManager().getProcessItems();
                appsDetailList.setAppsIcons(list);
                appsDetailList.setIsBatteryInform(true, 40);
                appsDetailList.setVisibility(View.VISIBLE);
                mBottomBtn.setVisibility(View.VISIBLE);
                mBottomBtn.setEnabled(true);

                ((TextView) mViewRoot.findViewById(R.id.app_detail_text)).setText(imContext.getString(R.string.function_battery_detail_title));
                batteryDetailsAppNum.setText(list.size() + "");

            }
        });
    }

    @Override
    public void startStateHandler() {
        if (handlerListener != null) {
            handlerListener.startHandler(appsDetailList.getHasSelected());
        }

        PhoneAccelerateHelper.executeAccelerateTask(imContext, mList);
        if (batteryScanLayout.getVisibility() == View.VISIBLE) {
            appsList.clearAppAnimation(new AnimationListenerAdapter() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    if (imageView != null) {
                        imageView.setVisibility(View.GONE);
                    }
                    appsList.setVisibility(View.GONE);
                    batteryScanLayout.setVisibility(View.GONE);
                    mViewRoot.setVisibility(View.GONE);
                    batteryScanView.onDestroyView();
                    batteryScanView.setVisibility(View.GONE);
                    if (handlerListener != null) {
                        handlerListener.finishHandler();
                    }
                }
            });
        } else {
            appsDetailList.clearAppAnimation(new AnimationListenerAdapter() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    appsDetailList.setVisibility(View.GONE);
                    batteryDetailsLayout.setVisibility(View.GONE);
                    mViewRoot.setVisibility(View.GONE);
                    FunctionItemSharedPrefs.setIsBatteryNormal(imContext, true);
                    if (handlerListener != null) {
                        handlerListener.finishHandler();
                    }
                }
            });
        }
        if (appsDetailList.getHasSelected()) {
            FunctionItemSharedPrefs.setFunctionItemOverHandingTime(imContext, FunctionItem.BATTERYOVER);
        }
    }

    @Override
    public void startStateFakeHandler() {
        if (handlerListener != null) {
            handlerListener.startFakeScan();
        }

        imContext.getAdSceneManager().load(EntranceType.HOME_POWER_INTERSTITIAL);
        FunctionItemSharedPrefs.setFunctionItemOverHandingTime(imContext, FunctionItem.BATTERYOVER);

        mViewRoot.setVisibility(View.VISIBLE);
        batteryScanLayout.setVisibility(View.VISIBLE);
        batteryScanView.startScanBattery(FunctionItemSharedPrefs.getBatteryPercent(imContext),
                true, new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        batteryNotification.setText(R.string.battery_sharpdec_state_normal_msg);
                        showMessageTop();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                batteryScanView.onDestroyView();
                                mViewRoot.setVisibility(View.GONE);
                                if (handlerListener != null) {
                                    handlerListener.finishHandler();
                                }
                            }
                        }, 1500);
                    }


                });
    }

    private void showMessageTop() {
        Animation fadeIn = AnimationUtils.loadAnimation(imContext, R.anim.battery_sharp_normal_shadein);
        batteryNotification.setVisibility(View.VISIBLE);
        batteryNotification.startAnimation(fadeIn);
    }

    @Override
    public void onDestroyView() {
        if (batteryScanView != null) {
            batteryScanView.onDestroyView();
        }
        super.onDestroyView();
    }
}
